package com.bnq.logc.tmp

import com.bnq.logc.config.LogcConfig
import com.bnq.logc.utils.ExecutorUtil
import com.bnq.logc.work.IScheduledUpload
import java.util.concurrent.TimeUnit

class TmpFileScheduleUpload(val config: LogcConfig.TmpScheduleUploadFileConfig) : IScheduledUpload {

    private var id:String = "tmp_scheduled_upload-" + config.subToken

    @Synchronized
    override fun schedule(time: Long, unit: TimeUnit) {
        val task = ExecutorUtil.getInstance().getScheduleTask(id)
        task?.cancel()
        val initialDelay = if(config.isNow) 0 else config.time
        ExecutorUtil.getInstance().scheduleAtFixedRate(TmpFileUploadTask(config),initialDelay,time,unit)
    }

    override fun cancel() {
        ExecutorUtil.getInstance().getScheduleTask(id)?.cancel()
    }
}